#include "componentdialog.h"
#include "componentgeneral.h"
#include "librarymodel.h"
#include "qedalibrary.h"
#include "qedacomponent.h"

#include <QtCore/qdebug.h>
#include <QtGui/qboxlayout.h>
#include <QtGui/qcombobox.h>
#include <QtGui/qdialogbuttonbox.h>
#include <QtGui/qgroupbox.h>
#include <QtGui/qformlayout.h>
#include <QtGui/qlineedit.h>
#include <QtGui/qpushbutton.h>

ComponentDialog::ComponentDialog(LibraryModel *libModel, QWidget *parent) :
    QDialog(parent)
{
    m_general = new ComponentGeneral(libModel);
    connect(m_general, SIGNAL(dataChanged()), this, SLOT(validate()));

    QGroupBox *generalGroup = new QGroupBox(tr("General"));
    generalGroup->setLayout(m_general);

    m_buttonBox = new QDialogButtonBox(QDialogButtonBox::Ok
                                     | QDialogButtonBox::Cancel);

    connect(m_buttonBox, SIGNAL(accepted()), this, SLOT(accept()));
    connect(m_buttonBox, SIGNAL(rejected()), this, SLOT(reject()));

    QVBoxLayout *mainLayout = new QVBoxLayout;
    mainLayout->addWidget(generalGroup);
    mainLayout->addWidget(m_buttonBox);
    setLayout(mainLayout);

    validate();
}

void ComponentDialog::dataFrom(QedaComponent *component)
{
    m_general->dataFrom(component);
    validate();
}

void ComponentDialog::dataTo(QedaComponent *component)
{
    m_general->dataTo(component);
}

QedaLibrary *ComponentDialog::library()
{
    return m_general->library();
}

void ComponentDialog::setGroup(const QString &group)
{
    m_general->setGroup(group);
}

void ComponentDialog::setLibrary(QedaLibrary *library)
{
    m_general->setLibrary(library);
}

void ComponentDialog::setReference(const QString &reference)
{
    m_general->setReference(reference);
}

void ComponentDialog::validate()
{
    m_buttonBox->button(QDialogButtonBox::Ok)->setEnabled(m_general->valid());
}
